package com.zhhtech.zhh.ui.activity.commom

import android.view.View
import com.xuexiang.xupdate.utils.UpdateUtils
import com.zhhtech.zhh.BuildConfig
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.common.PRIVACY_POLICY
import com.zhhtech.zhh.common.UserCache
import com.zhhtech.zhh.databinding.ActivitySettingBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.dialog.showAlertDialog
import com.zhhtech.zhh.ui.ext.startActivityExt
import com.zhhtech.zhh.ui.ext.toastMessageLong
import com.zhhtech.zhh.ui.helper.AppUpdateHelper
import com.zhhtech.zhh.ui.utils.SPUtils
import com.zhhtech.zhh.utils.GlideCatchUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class SettingActivity : BaseActivity<ActivitySettingBinding>() {
    override fun initView() {
        bindViewClick(
            vb.rlCleanCache, vb.buttonLogout, vb.tvVersion,
            vb.rlPrivacyPolicy, vb.buttonCancellation
        )
        vb.tvVersion.text = BuildConfig.VERSION_NAME
        vb.cbPush.isChecked = SPUtils.getBoolean("key_push", true)
        vb.cbPush.setOnClickListener {
            SPUtils.put("key_push", vb.cbPush.isChecked)
        }
    }

    override fun initData() {
        launchUI({
            vb.tvCache.text = withContext(Dispatchers.IO) {
                GlideCatchUtil.getCacheSize()
            }
        })
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.rlCleanCache -> showAlertDialog {
                message(message = "确定清除缓存吗？")
                negativeButton { }
                positiveButton {
                    GlideCatchUtil.cleanCatchDisk()
                    vb.tvCache.text = "0KB"
                    "清除成功".toastMessageLong()
                }
            }

            vb.buttonLogout -> {
                showAlertDialog {
                    message(message = "确定退出登录吗？")
                    negativeButton { }
                    positiveButton {
                        UserCache.logout()
                        startActivityExt<LoginActivity>()
//                        startActivityExt<MainActivity> {
//                            putExtra("position", 0)
//                        }
                    }
                }
            }

            vb.tvVersion -> {
                // 重置忽略更新
                UpdateUtils.saveIgnoreVersion(mThis, "")
                AppUpdateHelper.manualCheckUpdate(mThis)
            }

            vb.rlPrivacyPolicy -> openUrl(PRIVACY_POLICY)
            vb.buttonCancellation -> {
                showAlertDialog {
                    message(message = "确定注销账号吗？")
                    negativeButton { }
                    positiveButton {
                        launchUI({
                            val tips = api.userLogOut().toData()
                            tips?.tips?.toastMessageLong()
                            UserCache.logout()
                            startActivityExt<LoginActivity>()
                        }, ApiObserver(mThis))
                    }
                }
            }
        }
    }
}